// 优化axios的做法
import Vue from 'vue'
import axios from 'axios'
axios.defaults.baseURL = 'http://203.195.181.208:8888'
// Vue.prototype.$http = axios

/* get请求：axios.get(url,params:{})
post请求：axios.post(url,{}) */
// 获取轮播图数据
const getLunbo =  params => {
  // return axios.get('/api/getlunbo')
  return axios.get('/api/getlunbo',params)
}
// 获取九宫格数据
const getGrids =  params => {
  return axios.get('/api/grids',params)
}
// 获取新闻资讯数据
const getNewsList =  params => {
  return axios.get('/api/getnewslist',params)
}
// 根据id获取图文资讯详情
const getNewsInfoById =  params => {
  // return axios.get('/api/getnew/:newid')
  return axios.get('/api/getnew/' + params)
}
// 根据id获取评论信息
// 给默认值，默认limit为3
const getCommentsById =  ({artid,page,pageSize=3}) => {
  // return axios.get('/api/getcomments/:artid?pageindex=1&limit=3')
  return axios.get(`/api/getcomments/${artid}?pageindex=${page}&limit=${pageSize}`)
}
// 根据id添加评论信息
const postCommentsById =  ({id,content}) => {
  // return axios.get('/api/postcomment/:artid')
  return axios.post(`/api/postcomment/` + id,{content})
}
// 获取图片分类数据
const getImgCategory =  params => {
  return axios.get('/api/getimgcategory',params)
}
// 根据id获取图片数据
const getImagesById =  params => {
  // /api/getimages/:cateid
  return axios.get('/api/getimages/' + params)
}
// 根据id获取图片详情数据
const getImagesInfoById =  params => {
  // /api/getimageInfo/:imgid
  return axios.get('/api/getimageInfo/' + params)
}
// 根据id获取图片详情页面中的缩略图数据
const getThumImagesById =  params => {
  // /api/getthumimages/:imgid
  return axios.get('/api/getthumimages/' + params)
}
// 获取商品列表数据
const getGoods =  ({page,pageSize=4}) => {
  // /api/getgoods?pageindex=number
  return axios.get(`/api/getgoods?pageindex=${page}&limit=${pageSize}`)
}
// 通过id获取商品参数区和价格，标题等数据
const getGoodsInfoById =  params => {
  // /api/goods/getinfo/:id
  return axios.get('/api/goods/getinfo/' + params)
}

// 暴露出去
Vue.prototype.$http = {
  getLunbo,
  getGrids,
  getNewsList,
  getNewsInfoById,
  getCommentsById,
  postCommentsById,
  getImgCategory,
  getImagesById,
  getImagesInfoById,
  getThumImagesById,
  getGoods,
  getGoodsInfoById
}